/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lifxue.jqda.run;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lifxue.jqda.data.bean.AppInfoBean;
import org.lifxue.jqda.util.GlobalConstant;
import org.lifxue.jqda.util.GlobalVar;

/**
 *
 * @author lifxue
 */
public class Command {

    private static Log logger = LogFactory.getLog(Command.class);

    public static boolean run(String strFile) {
        boolean ok = false;

        if (strFile == null || strFile.trim().equals("")) {
            return false;
        }
        //解析字符串
        String fileType = strFile.substring(strFile.lastIndexOf(".") + 1);
        String filePath = "";
        String fileName = "";
        int index = strFile.indexOf(GlobalConstant.QSFRAME_DELIMITER);
        if (index == -1) {
            fileName = "";
            filePath = "";
        } else {
            fileName = strFile.substring(0, index);
            //logger.info(fileName);
            filePath = strFile.substring(index + 2);
        }
        //判断文件是否存在
        File file = new File(filePath);
        if (!file.exists()) {
            return false;
        }

        //运行选定的程序
        if (fileType.equals("exe")) {//可执行文件
            ok = Command.runAppByFile(file);
        } else if (fileType.equals("bat")) {//批处理文件
            ok = Command.runBatByFile(file);
        }
        //增加文件使用次数
        Iterator i = GlobalVar.APPLICATIONS.entrySet().iterator();
        while (i.hasNext()) {
            java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
            String key = (String) entry.getKey(); //返回与此项对应的键
            List<AppInfoBean> list = (List<AppInfoBean>) entry.getValue();
            if (fileName.equals(key)) {
                for (int j = 0; j < list.size(); j++) {
                    AppInfoBean appinfo = list.get(j);
                    if (appinfo.getPath().equals(filePath)) {
                        appinfo.setVolumeOfUse(appinfo.getVolumeOfUse() + 1);
                    }
                }
            }
        }

        return ok;
    }

    /**
     * 根据文件全路径启动程序
     * @param strFile
     * @return
     */
    private static boolean runAppByFile(File file) {

        try {
            //设置工作目录
            ProcessBuilder proc = new ProcessBuilder(file.getPath());
            proc.directory(file.getParentFile());
            proc.start();
        } catch (Exception e) {
            logger.debug(e);
        }
        return true;
    }

    /**
     * 通过全称来运行批处理文件（.bat）
     * @param strFile
     * @return
     */
    private static boolean runBatByFile(File file) {
        try {
            ProcessBuilder proc = new ProcessBuilder("cmd", "/c", file.getPath());
            //设置工作目录
            proc.directory(file.getParentFile());
            proc.start();
        } catch (Exception e) {
            logger.debug(e);
        }
        return true;


    }
}
